package org.open.otalk.api.r1.packet;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.open.otalk.aidl.IPacketExtension;
public  class DefaultPacketExtension extends IPacketExtension.Stub{

	 private String elementName;
	    private String namespace;
	    private Map<String,String> map;

	    /**
	     * Creates a new generic packet extension.
	     *
	     * @param elementName the name of the element of the XML sub-document.
	     * @param namespace the namespace of the element.
	     */
	    public DefaultPacketExtension(String elementName, String namespace) {
	        this.elementName = elementName;
	        this.namespace = namespace;
	    }

	     /**
	     * Returns the XML element name of the extension sub-packet root element.
	     *
	     * @return the XML element name of the packet extension.
	     */
	    public String getElementName() {
	        return elementName;
	    }

	    /**
	     * Returns the XML namespace of the extension sub-packet root element.
	     *
	     * @return the XML namespace of the packet extension.
	     */
	    public String getNamespace() {
	        return namespace;
	    }

	    public String toXML() {
	        StringBuilder buf = new StringBuilder();
	        buf.append("<").append(elementName).append(" xmlns=\"").append(namespace).append("\">");
	        for (String name : getNames()) {
	            String value = getValue(name);
	            buf.append("<").append(name).append(">");
	            buf.append(value);
	            buf.append("</").append(name).append(">");
	        }
	        buf.append("</").append(elementName).append(">");
	        return buf.toString();
	    }

	    /**
	     * Returns an unmodifiable collection of the names that can be used to get
	     * values of the packet extension.
	     *
	     * @return the names.
	     */
	    public synchronized Collection<String> getNames() {
	        if (map == null) {
	            return Collections.emptySet();
	        }
	        return Collections.unmodifiableSet(new HashMap<String,String>(map).keySet());
	    }

	    /**
	     * Returns a packet extension value given a name.
	     *
	     * @param name the name.
	     * @return the value.
	     */
	    public synchronized String getValue(String name) {
	        if (map == null) {
	            return null;
	        }
	        return map.get(name);
	    }

	    /**
	     * Sets a packet extension value using the given name.
	     *
	     * @param name the name.
	     * @param value the value.
	     */
	    public synchronized void setValue(String name, String value) {
	        if (map == null) {
	            map = new HashMap<String,String>();
	        }
	        map.put(name, value);
	    }
}
